<?php
namespace Admin\Controller;

use Think\Controller;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/8 0008
 * Time: 15:01
 */
class GoodsController extends Controller
{

    public function ajaxDelPic()
    {
        $picid = I('get.picid');
        $gpModel = D('goods_pic');
        $pic = $gpModel->field('pic,sm_pic,mid_pic,big_pic')->find($picid);
        deleteImage($pic);
        $gpModel->delete($picid);
    }

    //显示和处理表单
    public function add()
    {
        //判断用户是否提交了表单
        if (IS_POST) {
            $model = D('goods');
            /**
             * creatr() 2个参数
             * 1 接受的参数
             * 2 表单的类型 1添加 2修改
             */
            if ($model->create(I('post.'), 1)) {
                if ($model->add()) {
                    $this->success('操作成功', U('lst'));
                    exit;
                }
            }
            $error = $model->getError();
            $this->error($error);
        }
        $mlmoudle = D('member_level');
        $mldata = $mlmoudle->select();
        $brandModel = D('brand');
        $brandData = $brandModel->select();
        $resdata = array(
            'mldata' => $mldata,
            'brandData' => $brandData,
            '_page_title' => '添加新商品',
            '_page_btn_name' => '商品列表',
            '_page_btn_link' => U('lst'),
        );
        $this->assign(
            $resdata
        );
        $this->display();

    }

    public function lst()
    {
        $model = D('goods');
        $data = $model->search(10);
        $this->assign($data);
        $this->display();
    }

    //显示和处理表单
    public function edit()
    {
        $id = I('get.id');
        $model = D('goods');
        if (IS_POST) {
            /**
             * creatr() 2个参数
             * 1 接受的参数
             * 2 表单的类型 1添加 2修改
             */
            if ($model->create(I('post.'), 2)) {
                if ($model->save()) {
                    $this->success('操作成功', U('lst'));
                    exit;
                }
            }
            $error = $model->getError();
            $this->error($error);
        }

        $data = $model->where("id=$id")->find();
        $brandModel = D('brand');
        $mlModle = D('member_price');
        $mlData = $mlModle->field("b.level_name,a.price,a.level_id,a.goods_id")->alias('a')->join('LEFT JOIN __MEMBER_LEVEL__ b ON a.level_id = b.id')->where('goods_id=' . "$id")->select();
        $brandData = $brandModel->select();
        // 取出相册中现有的图片
        $gpModel = D('goods_pic');
        $gpData = $gpModel->field('id,mid_pic')->where(array(
            'goods_id' => array('eq', $id),
        ))->select();
        $resdata = array(
            'mlData' => $mlData,
            'brandData' => $brandData,
            'gpData' => $gpData,
            '_page_title' => '添加新商品',
            '_page_btn_name' => '商品列表',
            '_page_btn_link' => U('lst'),
            'data' => $data
        );
//        foreach ($brandData as $item) {
//            var_dump($item['id']);
//            echo "<br/>";
//        }
//        die;
//        for ($i = 0;$i < count($brandData);$i++){
//            var_dump($brandData[$i]['id']) ;
//            echo 1;
//            echo "<br/>";
//        }
//        die;
        $this->assign($resdata);
        $this->display();
    }

    public function delete()
    {
        $id = I('get.id');
        $model = D('goods');
        $oldlogo = $model->where("id = $id")->field('logo,mbig_logo,big_logo,mid_logo,sm_logo')->find();
        deleteImage($oldlogo);
        $res = $model->where(array(
            'id' => $id
        ))->delete();
        if ($res && $res == 1) {
            $this->success('操作成功', U('lst'));
        }
    }

}